var AfterSell = function(){
	
	var userSelector = null;
	var selectUserId = [];
	
	var userSelector2 = null;
	var selectUserId2 = [];
	var project_id ='-1';
	var ps = new ProjectSource();	
	// 查询条件---begin
	var searchName = new Ext.form.TextField({
		fieldLabel : '项目名称',
		width : 150
	});
	
	var searchArea = new Ext.form.ComboBox( {
		fieldLabel : '归属区域',
		//hiddenName : 'type',
		store: ps.getGridArea ,
		mode : 'local',
		displayField : 'NAME',
		triggerAction : 'all',
		editable : false,
		valueField : 'AREA_ID',
		typeAhead : true,
		width : 150,
		emptyText : '---请选择---',
		selectOnFocus : true
	});
	
	var searchComplateDate = new Ext.form.DateField( {
		fieldLabel : '竣工时间',
		format : 'Y-m-d',
		width : 150
	});
	
	var btn_search = new Ext.Button({
		text : '查询',
		iconCls: 'icon-search',
		handler: function (){searchProject();}
	});
	// 查询条件---end
	
	// 跟踪记录信息---begin

	var projectCode = new Ext.form.TextField({
		fieldLabel : '项目编号',
		width : 150
	});
	
	var projectName = new Ext.form.TextField({
		fieldLabel : '项目名称',
		width : 300,
		allowBlank : false
	});
	
	var projectArea = new Ext.form.ComboBox( {
		fieldLabel : '所属区域',
		//hiddenName : 'type',
		store :ps.getGridArea ,
		mode : 'local',
		displayField : 'NAME',
		triggerAction : 'all',
		editable : false,
		valueField : 'AREA_ID',
		typeAhead : true,
		width : 150,
		emptyText : '---请选择---',
		selectOnFocus : true,
		allowBlank : false
	});
	
	var projectType = new Ext.form.ComboBox( {
		fieldLabel : '项目类型',
		//hiddenName : 'type',
		store : OMS.PROJECTTYPE,
		mode : 'local',
		displayField : 'name',
		triggerAction : 'all',
		editable : false,
		valueField : 'id',
		typeAhead : true,
		width : 150,
		emptyText : '---请选择---',
		selectOnFocus : true,
		allowBlank : false
	});
	
	var projectBus = new Ext.form.TextArea({
		fieldLabel : '业务提供',
		width : 800
	});
	var projectCreateDate = new Ext.form.DateField( {
		fieldLabel : '开始实施时间',
		format : 'Y-m-d',
		width : 150
	});
	
	var projectComplateDate = new Ext.form.DateField( {
		fieldLabel : '计划结束时间',
		format : 'Y-m-d',
		width : 150
	});
	
	var planUper = new Ext.form.TextField({
		fieldLabel : '提交人',
		width : 150
	});
	
	//var projectPassDate = new Ext.form.DateField( {
	//	fieldLabel : '进展提交时间',
	//	format : 'Y-m-d H:i:s',
	//	width : 150,
	//	allowBlank : false
	//});
	var projectPassDate = new Ext.ux.form.DateTime({
		fieldLabel: '进展提交时间',
		timeFormat:'H:i:s',
        timeConfig: {
            altFormats:'H:i:s',
            allowBlank: true    
        },
        dateFormat:'Y-m-d',
        dateConfig: {
        	width:150,
            altFormats:'Y-m-d',
            allowBlank: false    
       }
	});
	
	
	var projectHeader = new Ext.form.TextField({
		fieldLabel : '参与领导',
		width : 150
	});
	
	var projectAssigner = new Ext.form.TextField({
		fieldLabel : '项目负责人',
		width : 150,
		allowBlank : false,
		listeners: {
			   'render': function(cmp) {
					var self = cmp.ownerCt.ownerCt;
					cmp.getEl().on('click', function(){showUser2();}, self); 
				}
		}
	});
	
	var projectMem = new Ext.form.TextField({
		fieldLabel : '项目组成员',
		width : 300,
		allowBlank : false,
		listeners: {
			   'render': function(cmp) {
					var self = cmp.ownerCt.ownerCt;
					cmp.getEl().on('click', function(){showUser();}, self); 
				}
		}
	});
	
	var projectNewlast = new Ext.form.TextArea({
		fieldLabel : '最新进展',
		width : 800
	});
	
	var projectDangerous = new Ext.form.TextArea({
		fieldLabel : '困难及支撑',
		width : 800
	});
	
	var projectNextPlan = new Ext.form.TextArea({
		fieldLabel : '下一步计划',
		width : 800
	});
	
/*	var fileUpload = new Ext.form.TextField({
		fieldLabel : '相关文件上传',
		width : 150,
		inputType : 'file'
	});*/
	var fileUpload = new Ext.form.FileUploadField({
		fieldLabel : '上传附件',
		emptyText: '请选择要上传的文件',
		width : 520,
		name : 'fileName',
		buttonText: '',
		buttonCfg: {
            iconCls: 'icon-compress'
        }
	});
	var strackStore = new Ext.data.JsonStore(
			{
				url : '../project/searchProjectTrack.form?timesnap=' + new Date().getTime(),
				baseParams: {projectId: '-1'},
				root : 'result',
				fields : [ 'PROJECT_ID', 'USER_ID', 'PROJECT_GROUP','LATEST_MESSAGE', 'DIFFICULT', 'NEXT_PLAN','CREATE_DT', 'FILE_PATH','USER_NAME'],
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						thiz.baseParams = { projectId : project_id };
					}
				}
			});
	var followGrid = new Ext.grid.GridPanel({
		width : 900,
		height : 160,
		store : strackStore,
		columns : [{header:'跟踪提交人',sortable: true,width: 150,dataIndex:'USER_NAME'},
		           {header:'跟踪提交时间',sortable: true,width: 150,dataIndex:'CREATE_DT'},
		           {header:'最新进展',sortable: true,width: 250,dataIndex:'LATEST_MESSAGE'},
		           {header:'困难及支撑',sortable: true,width: 250,dataIndex:'DIFFICULT'}],
		bbar:new Ext.PagingToolbar({
  		   pageSize: 5,
           store: strackStore,
           displayInfo: true,
           displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
           emptyMsg: "没有记录..."
          })
	});
	//下载 文件
	var downloadUrl = function(s, m, rec){
		return "<a href='../task/downloadFile.form?fileId="+s+"'>下载</a>";
	};
	//附件列表
	var fileStore = new Ext.data.JsonStore(
			{
				url : '../task/searchTaskUpLoadFile.form?timesnap='+new Date().getTime(),
				baseParams: {exeId: '-1'},
				root : 'result',
				fields: ['ACCESSORY_ID', 'ACCESSORY_NAME', 'UPLOAD_DATE','FILE_SIZE','UPLOAD_NAME','ACCESSORY_DESC','USER_NAME'],
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						thiz.baseParams = { exeId : project_id };
					}
				}
			});
	var fileGrid = new Ext.grid.GridPanel({
		width : 900,
		height : 150,
		store : fileStore,
		columns : [{header:'附件名称',sortable: true,width: 150,dataIndex:'ACCESSORY_NAME'},
		           {header:'上传日期',sortable: true,width: 150,dataIndex:'UPLOAD_DATE'},
		           {header:'文件大小(K)',sortable: true,width: 250,dataIndex:'FILE_SIZE'},
		           {header:'上传人',sortable: true,width: 250,dataIndex:'USER_NAME'},
		           {header: '操作',sortable: true,width: 50, dataIndex: 'ACCESSORY_ID',renderer:downloadUrl}
		           ],
   		bbar:new Ext.PagingToolbar({
   		   pageSize: 5,
            store: fileStore,
            displayInfo: true,
            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
            emptyMsg: "没有记录..."
           })
	});	
	// 跟踪记录信息---end
	var followRec = new Ext.form.FieldSet({
		title : '跟踪记录',
		collapsible : true,
		autoHeight : true,
		width : 930, 
		items : [followGrid,
		         {layout:'column',defaults:{layout:'form'},
		      		items:[{columnWidth:0.47,items:[projectCode,projectAssigner]},
		      		       {columnWidth:0.53,items:[projectName,projectMem]}
		             ]
		         },
		         {layout:'column',defaults:{layout:'form'},
		        	 items:[{columnWidth:0.47,items:[projectArea,projectCreateDate,projectHeader]},
			         {columnWidth:0.53,items:[projectType,projectPassDate,projectComplateDate]}
			         ]
		         },projectBus,projectNewlast,projectDangerous,projectNextPlan,fileUpload,fileGrid]
	});
	
	var beforeSellStore = new Ext.data.JsonStore(
			{
				url : '../project/searchProjects.form?status=21&timesnap=' + new Date()
						.getTime(),
				root : 'result',
				fields : [ 'PROJECT_ID', 'PROJECT_NAME', 'AREA_ID',
						'PROJECT_TYPE', 'EST_AMT', 'PROJECT_CREATE_DT',
						'EST_DT', 'JOIN_LEADER', 'CUSTOMER_NAME', 'STAFF_NUM',
						'CONTACT_PERSON', 'OFF_TEL', 'MOBILE', 'EMAIL',
						'PROJECT_TARGET', 'CUST_REQ', 'SERVICE_REQ',
						'IMP_FUNC', 'OWNER_PROVIDE', 'REMARK', 'COMPETITOR',
						'PROJECT_OWNER', 'INVOLVE_DT', 'ADVANTAGE',
						'DISADVANTAGE', 'CREATE_USER', 'CREATE_DT', 'MOD_USER',
						'MOD_DT' ],
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						thiz.baseParams = {
							searchName : searchName.getValue(),
							searchArea : searchArea.getValue(),
							searchComplateDate : searchComplateDate.getValue() ? searchComplateDate.getValue().format('Y-m-d') : ''
						};
					}
				}
			});
	//取得项目负责人信息路径
	var getProjectAssigner = new Ext.data.Connection({
		url:'../project/getProjectAssigner.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	var sellGrid = new Ext.grid.GridPanel({
		width : 930,
		height : 160,
		store : beforeSellStore,
		scope:this,
		listeners : {
			scope : this,
			rowmousedown : function(grid,index,e){
				var data = grid.store.getAt(index).data;
				setCompValueAndDisabledForBeforeSell(data);
				setProjectAssigner(data);
				project_id = data.PROJECT_ID;
				//跟踪信息
				strackStore.reload({params:{projectId:data.PROJECT_ID}});
				//附件信息
				fileStore.reload({params:{exeId:data.PROJECT_ID}});
				
			}
		},
		columns : [{header:'项目编号',sortable: true,width: 150,dataIndex:'PROJECT_ID'},
		           {header:'项目名称',sortable: true,width: 100, sortable: true,dataIndex:'PROJECT_NAME'},
		           {header:'所属区域',sortable: true,dataIndex:'AREA_ID',renderer:function(value,row){return ps.getGridAreaMap()[value];}},
		           {header:'项目类型',sortable: true,renderer:ps.renderprojectType,dataIndex:'PROJECT_TYPE'},
		           {header:'业务提供',sortable: true,width: 170,dataIndex:'OWNER_PROVIDE'},
		           {header:'开始时间',sortable: true,dataIndex:'PROJECT_CREATE_DT'},
		           {header:'计划结束时间',sortable: true,dataIndex:'EST_DT'},
		           {header:'估算金额',sortable: true,renderer: 'usMoney',dataIndex:'EST_AMT'}],
       bbar:new Ext.PagingToolbar({
		 pageSize: 5,
         store: beforeSellStore,
         displayInfo: true,
         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
         emptyMsg: "没有记录..."
       })
	});

	this.beforeSell = new Ext.form.FormPanel({
		frame : true,
		width : 950,
		labelWidth : 85,
		fileUpload: true, 
		buttons : [
		           {text:'保存',iconCls: 'icon-accept',scope:this,handler:function(){saveTract(this.beforeSell);}},
		           {text:'重置',iconCls: 'icon-return',scope:this,handler:function(){this.beforeSell.getForm().reset();}},
		           {text:'竣工',iconCls: 'icon-accept',scope:this,handler:function(){doCompleteProject(this.beforeSell);}}],
		items : [{layout:'column',defaults:{layout:'form'},
			      items:[{columnWidth:0.3,items:[searchName]},
			             {columnWidth:0.3,items:[searchArea]},
			             {columnWidth:0.3,items:[searchComplateDate]},
			             {columnWidth:0.1,items:[btn_search]}]
				},sellGrid,followRec],
	   listeners: {
	    	render: {fn:function(){ 
					ps.getGridArea.load();
					beforeSellStore.load();
					//strackStore.load();
			}}
		}
	});
	//为售前控件设置值
	var setCompValueAndDisabledForBeforeSell = function(data){
		projectCode.setValue(data.PROJECT_ID);
		projectName.setValue(data.PROJECT_NAME);
		projectArea.setValue(data.AREA_ID);
		projectType.setValue(data.PROJECT_TYPE);
		projectCreateDate.setValue(data.PROJECT_CREATE_DT);
		projectComplateDate.setValue(data.EST_DT);
		projectHeader.setValue(data.JOIN_LEADER);
		projectBus.setValue(data.OWNER_PROVIDE);
		
		projectCode.disable();
		projectName.disable();
		projectArea.disable();	
		projectType.disable();	
		projectCreateDate.disable();	
		projectComplateDate.disable();	
		projectHeader.disable();
		projectBus.disable();
	};
	//项目负责人控件设置值
	var setProjectAssigner = function(data){
		Utils.request(getProjectAssigner,{
			params:{
				project_id:data.PROJECT_ID
			},
			success:function(dataResult){
				projectAssigner.setValue(dataResult.projectAssigner);
				projectMem.setValue(dataResult.result);
				selectUserId2 = [dataResult.projectAssignerUserIds];
				selectUserId = [dataResult.userNamesUserIds];
			}
		});
	};
	//确定立项信息路径
	var addTractCon = new Ext.data.Connection({
		url:'../project/saveTractInfo.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	var saveTract = function(bs){
		var projectId = projectCode.getValue();
		if(projectId!=''){
			if(!bs.getForm().isValid()){
				Utils.info("有必填项没有填写，请检查！","系统提示");
				return false;
			}else{
				//Utils.request(addTractCon,{
				bs.getForm().submit({
					url:'../project/saveTractInfo.form?timesnap=' + new Date().getTime(),
					method:'post',
					params:{
						project_id:projectId,
						projectAssigner:selectUserId2.join(','),
						projectMem:selectUserId.join(','),
						projectPassDate:projectPassDate.getValue()?projectPassDate.getValue().format('Y-m-d H:i:s'):'',
						projectNewlast:projectNewlast.getValue(),
						projectDangerous:projectDangerous.getValue(),
						projectNextPlan:projectNextPlan.getValue(),
						fileName:fileUpload.getValue()
					},
					success:function(data){
						Utils.info("操作成功","系统提示");	
						strackStore.reload({params:{projectId:projectCode.getValue()}});
						fileStore.reload({params:{exeId:projectCode.getValue()}});

					}
				});
			}
		}else{
			Utils.info("请单击选择一个项目","系统提示");	
		}
	};
	
	//确定立项信息路径
	var completeProjectCon = new Ext.data.Connection({
		url:'../project/doCreateProject.form?timesnap=' + new Date().getTime(),
		method:'post'
	});
	var doCompleteProject = function(dp){
		var projectId = projectCode.getValue();
		if(projectId!=''){
			Utils.request(completeProjectCon,{
				params:{
					project_id:projectId,
					project_status:30
				},
				success:function(data){
					if(data.result=='true'){
						Utils.info("操作成功","系统提示");	
						beforeSellStore.load();
						dp.getForm().reset();
					}else{
						Utils.info("操作失败","系统提示");	
					}
				}
			});
		}else{
			Utils.info("请选择确定要竣工的项目","系统提示");	
		}
	};
	
	
	
	//检索项目
	var searchProject = function(){
		beforeSellStore.reload();
	};
	var showUser = function(){
		if(userSelector==null){
			userSelector=new Tree.User(selectUserId,function(o){
				setUsers(o);
			},null,'104');
		}
		userSelector.show();
	};
	var setUsers = function(o){
		var names = [];
		selectUserId =[];
		for(var i=0;i<o.length;i++) {
			names.push(o[i].text);
			selectUserId.push(o[i].id);
		}
		projectMem.setValue(names.join(';'));
	};
	
	var showUser2 = function(){
		if(userSelector2==null){
			userSelector2=new Tree.User(selectUserId2,function(o){
				setUsers2(o);
			},null,'103');
		}
		userSelector2.show();
	};
	var setUsers2 = function(o){
		var names = [];
		selectUserId2 =[];
		for(var i=0;i<o.length;i++) {
			names.push(o[i].text);
			selectUserId2.push(o[i].id);
		}
		projectAssigner.setValue(names.join(';'));
	};
};